<template>
  <el-dialog
    :title="id ? $t('common.modify') : $t('common.add')"
    :visible.sync="dialogVisible"
    width="500px"
    :show="show"
    :before-close="handleClose"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    @open="open"
  >
    <el-form
      ref="form"
      :model="info"
      label-width="90px"
      :rules="rules"
      v-loading="infoLoading"
    >
      <div class="el-dialog-div form-scrollbar">
        <el-form-item :label="$t('common.name')" prop="name">
          <el-input
            type="text"
            v-model="info.name"
            :placeholder="$t('common.enter', [$t('customer.sourceName')])"
            maxlength="255"
          />
        </el-form-item>
      </div>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-row>
        <el-col :span="12" class="btnEdit">
          <el-button :disabled="infoLoading" type="primary" @click="subInfo">{{
            $t('common.save')
          }}</el-button>
        </el-col>
        <el-col :span="12" class="btnEdit">
          <el-button @click="handleClose">{{ $t('common.cancel') }}</el-button>
        </el-col>
      </el-row>
    </div>
  </el-dialog>
</template>

<script>
import { createSource, getSource, updateSource } from '@/api/customer/source'

export default {
  name: 'edit',
  props: {
    show: {
      type: Boolean,
      default: false,
    },
    id: '',
    closeAction: '',
  },
  data() {
    return {
      dialogVisible: this.show,
      isChange: false,
      initForm: false,
      //页面元素
      info: {},
      tempData: {
        name: '',
      },
      infoLoading: true,
      rules: {
        //element form表单元素验证
        name: [
          {
            required: true,
            message: this.$t('common.enter', [this.$t('customer.sourceName')]),
            trigger: 'blur',
          },
        ],
      },
    }
  },
  methods: {
    async open() {
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
      if (this.id != '') {
        const { data } = await getSource(this.id)
        this.info = data
      } else {
        this.resetTempData()
        this.info = this.tempData
      }
      this.initForm = true
      setTimeout(() => {
        this.infoLoading = false
      }, 0.5 * 1000)
    },
    subInfo() {
      this.$refs['form'].validate(async (valid) => {
        if (valid) {
          this.infoLoading = true
          let data = this.info
          if (this.id != '') {
            await updateSource(this.id, data)
              .then((res) => {
                this.$notify({
                  title: this.$t('common.success'),
                  message: this.$t('common.updateSuccessfully'),
                  type: 'success',
                  duration: 2000,
                })
                this.$emit('closeAction')
              })
              .catch(() => {
                setTimeout(() => {
                  this.infoLoading = false
                }, 0.5 * 1000)
              })
          } else {
            await createSource(data)
              .then((res) => {
                this.$notify({
                  title: this.$t('common.success'),
                  message: this.$t('common.addSuccessfully'),
                  type: 'success',
                  duration: 2000,
                })
                this.$emit('closeAction')
              })
              .catch(() => {
                setTimeout(() => {
                  this.infoLoading = false
                }, 0.5 * 1000)
              })
          }
        }
      })
    },
    resetTempData() {
      this.tempData = {
        name: '',
      }
    },
    handleClose(done) {
      if (this.isChange) {
        this.$confirm(
          this.$t('common.unsavedChangesTip'),
          this.$t('common.tips'),
          {
            type: 'warning',
          }
        )
          .then((_) => {
            this.$emit('update:show', false)
          })
          .catch((_) => {})
      } else {
        this.$emit('update:show', false)
      }
    },
  },
  watch: {
    show() {
      this.dialogVisible = this.show
    },
    tempData: {
      handler: function () {
        if (this.initForm) {
          this.isChange = false
          this.initForm = false
        } else {
          this.isChange = true
        }
      },
      deep: true,
    },
  },
}
</script>

<style scoped>
.form-scrollbar {
  height: 200px !important;
}
</style>
